package admin

import (
	"object/go_cms/models"
)

type GroupAdminController struct {
	BaseAdminController
	models models.GroupModels
}

//用户组主页
// @router /admin/group/indexUI [get]
func (c *GroupAdminController) IndexUI() {
	c.TplAdmin("group/index")
}

// @router /admin/group/addUI [get]
func (c *GroupAdminController) AddUI() {
	c.TplAdmin("group/add")
}

// @router /admin/group/editUI [get]
func (c *GroupAdminController) EditUI() {
	c.TplAdmin("group/edit")
}

// @router /admin/group/accessUI [get]
func (c *GroupAdminController) AccessUI() {
	c.TplAdmin("group/access")
}

// @router /admin/group/get [get,post]
func (c *GroupAdminController) Get() {
	c.Res.Code = 1
	id, err := c.GetInt("id")
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = "参数错误"
	} else {
		c.Res.Data, err = c.models.Get(id)
		if err != nil {
			c.Res.Code = 0
			c.Res.Msg = err.Error()
		}
	}
	c.Json(c.Res)
}

// @router /admin/group/query [get,post]
func (c *GroupAdminController) Query() {
	c.Res.Code = 0
	c.Res.Data, _ = c.models.Query()
	c.Json(c.Res)
}

// @router /admin/group/add [post]
func (c *GroupAdminController) Add() {
	c.Res.Code = 1
	c.Res.Msg = "添加成功"
	maps := c.GetParamMap()
	row, err := c.models.Add(maps)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = err.Error()
	} else {
		c.Res.Data = row
	}
	c.Json(c.Res)
}

// @router /admin/group/edit [post]
func (c *GroupAdminController) Edit() {
	c.Res.Code = 1
	c.Res.Msg = "修改成功"
	maps := c.GetParamMap()
	row, err := c.models.Edit(maps)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = err.Error()
	} else {
		c.Res.Data = row
	}
	c.Json(c.Res)
}

// @router /admin/group/delete [post]
func (c *GroupAdminController) Delete() {
	c.Res.Code = 1
	c.Res.Msg = "删除成功"
	id, err := c.GetInt("id")
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = "参数错误"
	} else {
		row, err2 := c.models.Delete(id)
		if err2 != nil {
			c.Res.Code = 0
			c.Res.Msg = err2.Error()
		} else {
			c.Res.Data = row
		}
	}
	c.Json(c.Res)
}

// @router /admin/group/getAccess [post]
func (c *GroupAdminController) GetAccess() {
	gid, _ := c.GetInt64("group_id")

	gm := models.GroupMenuModels{}
	c.Res.Data, _ = gm.Query(gid)
	c.Res.Code = 1
	c.Json(c.Res)
}

// @router /admin/group/updateAccess [post]
func (c *GroupAdminController) UpdateAccess() {
	maps := c.GetParamMap()
	c.Res.Code = 1
	c.Res.Msg = "保存成功"
	gm := models.GroupMenuModels{}
	_, err := gm.Update(maps)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = "保存失败"
	}
	c.Json(c.Res)
}
